﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;

namespace GlobalHelper
{
    public class Helper
    {
        public readonly static DateTime BeginDate = new DateTime(2009, 1, 1);
        public readonly static DateTime EndDate = new DateTime(2019, 1, 1);

        public static readonly IList<string> FullWeek = new string[] { "周一至周日", "周一～周日", "周一—周日" };
        public static readonly IList<string> MondayToFirday = new string[] { "周一至周五", "平常", "平日", "周一～周五", "周一～～周五",
                                                                             "周一—周五","平   常",
        };
        public static readonly IList<string> SaturdayToSunday = new string[] { "周六至周日", "周六、周日", "周六周日", "周六—周日", "周六--周日",
                                                                               "周六、周日","周六.日","双    休","双休","周六～周日",
        };

        public static readonly string FulWeekUseSort = "1111111";
        public static readonly string MondayToFirdayUseSort = "0111110";
        public static readonly string SaturdayToSundayUseSort = "1000001";

        public static readonly string Level = "1";

        public static readonly int RootCompanyId = 61;
        public static readonly string DepartmentDisplayMember = "cDepName";
        public static readonly string DepartmentValueMember = "nDepID";
        public static readonly string LineDisplayMember = "cLineName";
        public static readonly string LineValueMember = "nLineID";

        public static readonly string TokenBeginFlag = "<";
        public static readonly string TokenEndFlag = ">";

        public static readonly string TokenLineBeginFlag = "路牌";
        public static readonly IList<string> TokenLineEndFlag = new string[] { "x", "X", "×", "入内", "～停", "*", "※", "入", "人", "停" };
        public static readonly string StandTokenEndFlag = "x";

        /// <summary>
        /// 判断DataTable是否为空
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool IsEmpty(DataTable dt)
        {
            bool result = false;

            if (dt == null
                || dt.Rows.Count <= 0)
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// 判断字符串是否纯数字
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool CheckIsNumber(string s)
        {
            var result = false;

            Regex regex = new Regex(@"^[0-9]*[0-9]*[0-9]$");

            if (regex.IsMatch(s))
            {
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 判断字符串是否含有小数的数字
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool CheckIsDecimal (string s)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(s);
        }

        public static DataTable CreateEmptyDataTableWithString(int rowNum, int colNum)
        {
            DataTable result = new DataTable();

            for (int i = 0; i < colNum; i++)
            {
                result.Columns.Add("", typeof(string));
            }
            for (int i = 0; i < rowNum; i++)
            {
                DataRow dataRow = result.NewRow();
                result.Rows.Add(dataRow);
            }

            return result;
        }

        public static DataTable AddNewColWithString(DataTable dataTable)
        {
            DataTable result = dataTable;

            //int colNum = dataTable.Columns.Count;

            //for (int i = 0; i < colNum; i++)
            //{
            result.Columns.Add("", typeof(string));
            //}

            return result;
        }

        /// <summary>
        /// 判断字符串是否在一个字符串数组中
        ///  ps:不包含空格
        /// </summary>
        /// <param name="list"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsInList(IList<string> list, string str)
        {
            if (list.Count <= 0)
            {
                return false;
            }
            if (string.IsNullOrEmpty(str))
            {
                return false;
            }

            bool result = false;


            foreach (string s in list)
            {
                if (s.Trim() == str.Trim())
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// 根据一个2维表生成DataTable
        /// </summary>
        /// <param name="tableInfo"></param>
        /// <param name="rowAmount"></param>
        /// <param name="colAmount"></param>
        /// <returns></returns>
        public static DataTable CreateDataTable(IList<IList<string>> tableInfo, int rowAmount, int colAmount)
        {
            int maxColAmount = 0;
            foreach (IList<string> list in tableInfo)
            {
                if (list.Count > maxColAmount)
                {
                    maxColAmount = list.Count;
                }
            }
            if (maxColAmount > colAmount || tableInfo.Count > rowAmount)
            {
                throw new Exception("数据输入异常,在2维表转DataTable时!");
            }

            DataTable result = Helper.CreateEmptyDataTableWithString(rowAmount, colAmount);

            //row
            for (int i = 0; i < tableInfo.Count; i++)
            {
                //col
                for (int j = 0; j < tableInfo[i].Count; j++)
                {
                    result.Rows[i][j] = tableInfo[i][j];
                }
            }

            return result;
        }

        /// <summary>
        /// DataRow 转 IList
        /// </summary>
        /// <param name="dataRow"></param>
        /// <returns></returns>
        public static IList<string> GetLine(DataRow dataRow)
        {
            IList<string> result = new List<string>();

            foreach (object o in dataRow.ItemArray)
            {
                result.Add(o.ToString());
            }

            return result;
        }

        /// <summary>
        /// 移除 字符串中所有非数字
        /// 保留数字
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveContentFlag(string content)
        {
            string result = content;

            string s = Regex.Replace(content, @"\D", "");

            if (!string.IsNullOrEmpty(s))
            {
                result = s;
            }

            return result;
        }
    }
}
